package com.m1905.lib.download.listener;

import android.os.Handler;
import android.os.Looper;


import com.m1905.lib.download.model.DataWrapper;
import com.m1905.lib.download.model.DownloadDBItem;
import com.m1905.lib.download.util.DLog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wanghao
 * @date 2023/10/24.
 * @des
 */
public class DispatchDownloadStatusListener implements DownloadStatusListener {
    private static final int STATUS_WAITING = 0;
    private static final int STATUS_START = 1;
    private static final int STATUS_FILE_SIZE = 2;
    private static final int STATUS_LOADING = 3;
    private static final int STATUS_SUCCESS = 4;
    private static final int STATUS_FAILURE = 5;
    private static final int STATUS_CANCEL = 6;
    private static final int STATUS_DELETE = 7;
    private static final int STATUS_CMD_COMPLETED = 8;
    private final Handler handler = new Handler(Looper.getMainLooper());

    private List<DownloadStatusListener> listeners = new ArrayList<>();

    public void registerDownloadStatusListener(DownloadStatusListener callBack) {
        if (!listeners.contains(callBack)) listeners.add(callBack);
    }

    public void unregisterDownloadStatusListener(DownloadStatusListener callBack) {
        listeners.remove(callBack);
    }

    @Override
    public void onWaiting(DownloadDBItem downloadItem, boolean insert) {
        postToMainLooper(downloadItem, STATUS_WAITING, insert, -1);
    }

    @Override
    public void onStart(DownloadDBItem downloadItem) {
        postToMainLooper(downloadItem, STATUS_START, false, -1);
    }

    @Override
    public void onFileSize(DownloadDBItem downloadItem) {
        postToMainLooper(downloadItem, STATUS_FILE_SIZE, false, -1);
    }

    @Override
    public void onLoading(DownloadDBItem downloadItem) {
        postToMainLooper(downloadItem, STATUS_LOADING, false, -1);
    }

    @Override
    public void onSuccess(DownloadDBItem downloadItem) {
        postToMainLooper(downloadItem, STATUS_SUCCESS, false, -1);
    }

    @Override
    public void onFailure(DownloadDBItem downloadItem) {
        postToMainLooper(downloadItem, STATUS_FAILURE, false, -1);
    }

    @Override
    public void onCancel(DownloadDBItem downloadItem) {
        postToMainLooper(downloadItem, STATUS_CANCEL, false, -1);
    }

    @Override
    public void onDelete(DownloadDBItem downloadItem) {
        postToMainLooper(downloadItem, STATUS_DELETE, false, -1);
    }

    @Override
    public void onCmdComplete(int cmd) {
        postToMainLooper(null, STATUS_CMD_COMPLETED, false, cmd);
    }

    public void dispatchComplete(DataWrapper wrapper) {
        postToMainLooper(null, STATUS_CMD_COMPLETED, false, wrapper.getCmd());
        if (Looper.myLooper() == Looper.getMainLooper()) {
            if (wrapper.getListener() != null) wrapper.getListener().onCmdCompleted();
        } else {
            if (wrapper.getListener() != null) {
                handler.post(() -> wrapper.getListener().onCmdCompleted());
            }
        }

    }

    private void postToMainLooper(DownloadDBItem downloadItem, int status, boolean insert, int cmd) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            dispatch(downloadItem, status, insert, cmd);
        } else {
            handler.post(() -> dispatch(downloadItem, status, insert, cmd));
        }
    }

    private static final String[] STATUS = {"STATUS_WAITING", "STATUS_START", "STATUS_FILE_SIZE", "STATUS_LOADING", "STATUS_SUCCESS", "STATUS_FAILURE", "STATUS_CANCEL", "STATUS_DELETE", "STATUS_CMD_COMPLETED"};

    private void dispatch(DownloadDBItem downloadItem, int status, boolean insert, int cmd) {
        DLog.e("DownloadManager dispatch " + STATUS[status] + "(" + status + ")  insert:" + insert + "  cmd:" + cmd);
        for (DownloadStatusListener listener : listeners) {
            switch (status) {
                case STATUS_WAITING:
                    listener.onWaiting(downloadItem, insert);
                    break;
                case STATUS_START:
                    listener.onStart(downloadItem);
                    break;
                case STATUS_FILE_SIZE:
                    listener.onFileSize(downloadItem);
                    break;
                case STATUS_LOADING:
                    listener.onLoading(downloadItem);
                    break;
                case STATUS_SUCCESS:
                    listener.onSuccess(downloadItem);
                    break;
                case STATUS_FAILURE:
                    listener.onFailure(downloadItem);
                    break;
                case STATUS_CANCEL:
                    listener.onCancel(downloadItem);
                    break;
                case STATUS_DELETE:
                    listener.onDelete(downloadItem);
                    break;
                case STATUS_CMD_COMPLETED:
                    listener.onCmdComplete(cmd);
                    break;
            }
        }
    }


}
